package com.zzyl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;


    /**
     * 分页查询所有护理计划
     * @param name
     * @param status
     * @param pageNum
     * @param pageSize
     */
    @Override
    public PageResponse<NursingPlanVo> selectPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        //设置开始分页页码
        PageHelper.startPage(pageNum, pageSize);

        //分页查询所有的护理计划
        Page<NursingPlanVo> page = nursingPlanMapper.selectPage(name, status);

        //封装数据并返回
        return PageResponse.of(page,NursingPlanVo.class);
    }

    /**
     * 新增护理计划
     * 需要完成对护理计划的新增和中间表的新增
     * @param nursingPlanDto
     */

    @Override
    @Transactional
    public void add(NursingPlanDto nursingPlanDto) {
        //1.创建一个NursingPlan对象 用来接受Dto里的和NursingPlan有关数据
        NursingPlan nursingPlan = new NursingPlan();

        //2.数据拷贝
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);

        //3.补充缺少的数据
        nursingPlan.setCreateTime(new Date());
        nursingPlan.setStatus(0);
        //4.向nursingPlan中写入数据
        nursingPlanMapper.insert(nursingPlan);

        //从封装主键id的nursingPlan对象中获取生成的主键id
        Integer planId = nursingPlan.getId();

        /*向中间表插入n条数据*/
        //1.从Dto中获取中间表对象
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();

        //2.给所有的projectPlan中间表的planId赋值
        projectPlans.forEach(projectPlan -> {
            projectPlan.setPlanId(planId);
            projectPlan.setCreateTime(LocalDateTime.now());
        });
        //3.向中间表写入数据
        nursingProjectPlanMapper.insertBatch(projectPlans);
    }

    /**
     * 根据Id查询护理计划信息
     * 需要查询护理计划和中间表
     * @param id
     * @return
     */
    @Override
    public NursingPlanVo getById(Integer id) {
        //1.获取nursingPlan的基本数据 id为护理计划Id
        NursingPlan nursingPlan = nursingPlanMapper.getById(id);
        //2.获取中间表的数据
        List<NursingProjectPlanVo> nursingProjectPlans = nursingProjectPlanMapper.getById(id);

        //3.封装数据
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        BeanUtils.copyProperties(nursingPlan, nursingPlanVo);
        nursingPlanVo.setProjectPlans(nursingProjectPlans);

        return nursingPlanVo;
    }

    /**
     * 根据Id修改护理计划和中间表
     * 需要修改护理计划和中间表
     * @param nursingPlanDto
     * @param id
     */
    @Override
    @Transactional
    public void update(NursingPlanDto nursingPlanDto, Integer id) {
        //1.修改护理计划信息
        NursingPlan nursingPlan = new NursingPlan();

        BeanUtils.copyProperties(nursingPlanDto,nursingPlan);
        nursingPlan.setId(id);
        nursingPlan.setUpdateTime(new Date());
        nursingPlanMapper.update(nursingPlan);
        //2.删除中间表信息
        nursingProjectPlanMapper.delById(id);
        //3.再向中间表中重新插入数据
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();

        projectPlans.forEach(projectPlan -> {
            projectPlan.setPlanId(id);
            projectPlan.setCreateTime(LocalDateTime.now());
        });
        nursingProjectPlanMapper.insertBatch(projectPlans);
    }

    /**
     * 根据Id删除
     * @param id
     */
    @Override
    @Transactional
    public void delete(Integer id) {
        nursingPlanMapper.delById(id);
        nursingProjectPlanMapper.delById(id);
    }

    /**
     * 启用或者禁用状态
     * @param id
     * @param status
     */
    @Override
    public void startOrStop(Integer id, Integer status) {
        NursingPlan nursingPlan = new NursingPlan();
        nursingPlan.setId(id);
        nursingPlan.setStatus(status);
        nursingPlanMapper.update(nursingPlan);
    }

    @Override
    public List<NursingPlanVo> getAllPlan() {
        List<NursingPlanVo> list = nursingPlanMapper.getAllPlan();
        return list;
    }
}
